package com.rebate.web.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.Merchant;
import com.rebate.service.IBaseService;
import com.rebate.util.Page;
import com.rebate.util.PageUtils;

@Controller
public class MerchantServlet extends BaseServlet{
	
	private String view = "/merchant/listmerchant";
	private String createView = "/merchant/createmerchant";
	private String editView = "/merchant/editmerchant";
	@Autowired
	private IBaseService<Merchant> merchantService;
	
	/**
	 * 商户列表页面
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/index.htm")
	public String merchantIndex(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		return view;
	}
	
	/**
	 * 创建商户页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/add.htm")
	public String addInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		return createView;
	}
	
	/**
	 * 创建商户
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/save.htm")
	public void saveMerchant(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		merchant.setCreateTime(new Date());
		Long id = merchantService.create(merchant);
		if(id != null) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 编辑商户页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/edit.htm")
	public String editInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		merchant = merchantService.findById(merchant.getId());
		model.put("merchant", merchant);
		return editView;
	}
	
	/**
	 * 更新商户信息
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/update.htm")
	public void updateMerchant(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		if(merchantService.update(merchant)) {
			response.getWriter().print(1);//update success
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 查询商户List
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/list.htm")
	public void getMerchantList(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		Page<Merchant> page = PageUtils.getPage(request);
		page = merchantService.queryForListPage(page, merchant);
		print(request, response, page);
	}
	/**
	 * remove merchanat
	 * @throws IOException 
	 */
	@RequestMapping(value = "/merchant/remove.htm")
	public void removeMerchant(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("merchant") Merchant merchant) throws IOException {
		if(merchantService.delete(merchant.getId())) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
}
